<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 练习-监控httpd进程 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 练习-监控httpd进程</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>在服务器上，写一个监控脚本。</p>
<ol>
<li><p>每隔10s去检测一次服务器上的httpd进程数，如果大于等于500的时候，就需要自动重启一下apache服务，并检测启动是否成功？</p>
</li>
<li><p>若没有正常启动还需再一次启动，最大不成功数超过5次则需要理解发邮件通知管理员，并且以后不需要再检测！ </p>
</li>
<li><p>如果启动成功后，1分钟后再次检测httpd进程数，若正常则重复之前操作（每隔10s检测一次），若还是大于等于500，那放弃重启并需要发邮件给管理员，然后自动退出该脚本。假设其中发邮件脚本为之前咱们使用的mail.py</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">check_service()</div><div class="line">&#123;</div><div class="line">    n=0</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 5`</div><div class="line">    <span class="keyword">do</span></div><div class="line">        /usr/<span class="built_in">local</span>/apache2/bin/apachectl restart 2&gt;/tmp/apache.err</div><div class="line">        <span class="keyword">if</span> [ $? -ne 0 ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">            n=$[<span class="variable">$n</span>+1]</div><div class="line">        <span class="keyword">else</span></div><div class="line">            <span class="built_in">break</span></div><div class="line">        <span class="keyword">fi</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$n</span> -eq 5 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="comment">##下面的mail.py参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py</span></div><div class="line">        python mai.py <span class="string">"123@qq.com"</span> <span class="string">"httpd service down"</span> `cat /tmp/apache.err`</div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span>  </div><div class="line">&#125;</div><div class="line">    </div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    t_n=`ps -C httpd --no-heading |wc -l`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$t_n</span> -ge 500 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        /usr/<span class="built_in">local</span>/apache2/bin/apachectl restart </div><div class="line">        <span class="keyword">if</span> [ $? -ne 0 ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">            check_service</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        sleep 60</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    sleep 10</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Shell 练习/16. shell 练习-监控httpd进程/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
